home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5133 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: pointers to pointers that point to structs?
  5. Date: Sat, 10 Feb 1996 06:08:19 GMT
  6. Organization: Netcom
  7. Message-ID: <311c3581.41348295@nntp.ix.netcom.com>
  8. References: <311C1B4E.217F@mars.superlink.net>
  9. NNTP-Posting-Host: ix-dc12-17.ix.netcom.com
  10. X-NETCOM-Date: Fri Feb 09 10:08:17 PM PST 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Michael Rizzo <rizzom@mars.superlink.net> wrote:
  14.  
  15. > Hi,
  16. >   I am having trouble dereferencing a pointer to a pointer to a struct.
  17. > For simplicity just say the struct is:
  18. > struct test
  19. >   {
  20. >   char teststr[10];
  21. >   int  testint;
  22. >   }
  23. > Now I have a function that just wants to print elements of the 
  24. > structure:
  25. > void f(test **temp)
  26. > {
  27. > printf("%s  %d",(please fill in the blank))
  28. > }
  29. > How to I get to test.teststr and test.testint from within the funtion.
  30. > I have tried temp->teststr.  I'm still pretty new to programming in C, 
  31. > and the book I'm using does not go into such topics, so any help would 
  32. > be greatly appreciated.  
  33.  
  34.     printf("%s  %d", (**temp).teststr, (**temp).testint);
  35.  
  36. or
  37.  
  38.     printf("%s  %d", (*temp)->teststr, (*temp)->testint);
  39.  
  40. The rule to remember is "if x is a pointer to foo, then *x is a foo."
  41. In your case temp is a pointer to pointer to struct test, so *temp is
  42. a pointer to struct test and **temp is a struct test.  To reference a
  43. member of a struct, you can use a pointer to struct (e.g., *temp) and
  44. "->" or a struct (e.g., **temp) and ".".
  45.  
  46.  
  47. Michael M Rubenstein
  48.